package de.hs_zigr.inf.iib08gruppe5powerfish.it.boundary;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;



/** List of Queries; manual saved and history */
public class GUIListOfQueries implements ActionListener {

	/** to hold lstQueries */
	private JScrollPane spLstQueries;
	/** to hold spLstQueries */
	private JPanel pnlQueryLst;
	/** a list of queries */
	private JList lstQueries;
	/** to hold pnlQueryLst */
	private JFrame frmQueryLst;
	/** stores XQueries for lstQueries */
	private DefaultListModel dlm;
	/** stores XQueries for lstQueriesHistory */
	private DefaultListModel dlmHistory;
	/** reference of guiQueryProg */
	private GUIQueryProg guiQueryProg;

	/** loads queries to dlm */
	public void loadLstOfQuerys(GUIQueryProg guiQueryProg) {
		this.guiQueryProg = guiQueryProg;
		try {
			dlm = guiQueryProg.getCtrlQueryProg().loadStoredQueries();
		} catch (IOException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(frmQueryLst,
					"Fatal Exception (QuiryLoader). Please contact support. ",
					"program error", JOptionPane.ERROR_MESSAGE);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(frmQueryLst,
					"Fatal Exception (QuiryLoader). Please contact support. ",
					"program error", JOptionPane.ERROR_MESSAGE);
		}
	}

	/** loads queries to dlmHistory */
	public void loadLstOfQuerysHistory(GUIQueryProg guiQueryProg) {
		this.guiQueryProg = guiQueryProg;
		try {
			dlmHistory = guiQueryProg.getCtrlQueryProg()
					.loadStoredQueriesHistory();
		} catch (IOException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(frmQueryLst,
					"Fatal Exception (QuiryLoader). Please contact support. ",
					"program error", JOptionPane.ERROR_MESSAGE);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(frmQueryLst,
					"Fatal Exception (QuiryLoader). Please contact support. ",
					"program error", JOptionPane.ERROR_MESSAGE);
		}
	}

	/**
	 * shows a list of stored queries
	 */
	public JFrame showLstOfQueries() {
		// if loadLstOfQuerys()
		if (dlm != null) {
			lstQueries = new JList(dlm);
		} else {
			// if loadLstOfQuerysHistory()
			lstQueries = new JList(dlmHistory);
		}
		lstQueries.setDropMode(DropMode.INSERT);
		MouseAdapter listener = new RecordListener(lstQueries);
		lstQueries.addMouseListener(listener);
		lstQueries.addMouseMotionListener(listener);
		lstQueries.setVisibleRowCount(3);
		frmQueryLst = new JFrame();
		frmQueryLst.setUndecorated(true);
		pnlQueryLst = new JPanel();
		pnlQueryLst.setBorder(BorderFactory.createLineBorder(Color.blue));
		pnlQueryLst.setLayout(new BorderLayout());
		Dimension dim;
		frmQueryLst.setSize(600, 500);
		dim = Toolkit.getDefaultToolkit().getScreenSize();
		frmQueryLst.setLocation((dim.width - 600), (dim.height - 400) / 2);
		// frmQueryLst.setResizable(true);
		frmQueryLst.setTitle("Queries");
		frmQueryLst.getContentPane().add(pnlQueryLst);
		frmQueryLst.setVisible(true);
		frmQueryLst.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		// pnlQueryLst.add(lstQueries, BorderLayout.CENTER);
		spLstQueries = new JScrollPane();
		spLstQueries.add(lstQueries);
		spLstQueries.getViewport().add(lstQueries);
		pnlQueryLst.add(spLstQueries, BorderLayout.CENTER);
		// highlights item on mouse-over
		lstQueries
				.addMouseMotionListener(new MyMouseAdapter(guiQueryProg, this) {
				});
		// to select an item when clicked and to close frmQueryLst
		lstQueries.addMouseListener(new MyMouseListener(guiQueryProg, this) {
		});
		return frmQueryLst;
	}

	/** gets list of Queries */
	public JList getLstQueries() {
		return lstQueries;
	}

	/** get frameQueryLst */
	public JFrame getFrmQueryLst() {
		return frmQueryLst;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
	}
}